package com.jyxd.web.controller.basic;

import com.alibaba.fastjson.JSON;
import com.jyxd.web.data.basic.BloodSugar;
import com.jyxd.web.data.dto.BloodSugarDTO;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.data.log.Log;
import com.jyxd.web.data.user.User;
import com.jyxd.web.service.assessment.basic.BloodSugarService;
import com.jyxd.web.service.log.LogService;
import com.jyxd.web.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;

@Api(tags = "病人血糖相关接口")
@Controller
@RequestMapping(value = "/bloodSugar")
public class BloodSugarController {

    private static Logger logger = LoggerFactory.getLogger(BloodSugarController.class);

    @Autowired
    private BloodSugarService bloodSugarService;

    @Autowired
    private LogService logService;

    /**
     * 增加一条血糖表记录
     *
     * @return
     */
    @RequestMapping(value = "/insert")
    @ResponseBody
    public String insert(@RequestBody Map<String, Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "添加失败");
        try {
            JSONArray array = JSONArray.fromObject(map.get("list").toString());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            if (array != null && array.size() > 0) {
                for (int i = 0; i < array.size(); i++) {
                    JSONObject obj = (JSONObject) array.get(i);
                    //有id 说明是编辑
                    if (StringUtils.isNotEmpty(obj.getString("id"))) {
                        BloodSugar bloodSugar = bloodSugarService.queryData(obj.getString("id"));
                        bloodSugar.setContent(obj.getString("content"));
                        bloodSugar.setCode(obj.getString("code"));
                        User user = (User) session.getAttribute("user");
                        if (user != null) {
                            //添加操作日志信息
                            Log log = new Log();
                            log.setId(UUIDUtil.getUUID());
                            log.setOperatorCode(user.getLoginName());
                            log.setOperateTime(new Date());
                            log.setMenuCode(MenuCode.XTDKJLR_CODE.getCode());
                            log.setContent(map.toString());
                            log.setOperateType(LogTypeCode.UPDATE_CODE.getCode());
                            log.setPatientId(obj.getString("patientId"));
                            logService.insert(log);
                        }
                        bloodSugarService.update(bloodSugar);
                    } else {
                        //id 为null 说明是新增
                        BloodSugar bloodSugar = new BloodSugar();
                        bloodSugar.setId(UUIDUtil.getUUID());
                        bloodSugar.setStatus(1);
                        bloodSugar.setCreateTime(new Date());
                        User user = (User) session.getAttribute("user");
                        if (user != null) {
                            bloodSugar.setOperatorCode(user.getLoginName());
                            //添加操作日志信息
                            Log log = new Log();
                            log.setId(UUIDUtil.getUUID());
                            log.setOperatorCode(user.getLoginName());
                            log.setOperateTime(new Date());
                            log.setMenuCode(MenuCode.XTDKJLR_CODE.getCode());
                            log.setContent(map.toString());
                            log.setOperateType(LogTypeCode.ADD_CODE.getCode());
                            log.setPatientId(obj.getString("patientId"));
                            logService.insert(log);
                        }
                        bloodSugar.setCode(obj.getString("code"));
                        bloodSugar.setContent(obj.getString("content"));
                        bloodSugar.setPatientId(obj.getString("patientId"));
                        bloodSugar.setVisitCode(obj.getString("visitCode"));
                        bloodSugar.setVisitId(obj.getString("visitId"));
                        bloodSugar.setDataTime(sdf.parse(obj.getString("dataTime")));
                        bloodSugarService.insert(bloodSugar);
                    }
                }
                json.put("code", HttpCode.OK_CODE.getCode());
                json.put("msg", "添加成功");
            }
        } catch (Exception e) {
            logger.info("增加一条血糖表记录:" + e);
        }
        return json.toString();
    }

    /**
     * 更新血糖表记录状态
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public String update(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "更新失败");
        if (map != null && map.containsKey("id") && map.containsKey("status")) {
            BloodSugar bloodSugar = bloodSugarService.queryData(map.get("id").toString());
            if (bloodSugar != null) {
                bloodSugar.setStatus((int) map.get("status"));
                bloodSugarService.update(bloodSugar);
                json.put("msg", "更新成功");
            } else {
                json.put("msg", "更新失败，没有这个对象。");
                return json.toString();
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 编辑血糖表记录单
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public String edit(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "编辑失败");
        if (map != null && map.containsKey("id") && map.containsKey("status") && map.containsKey("bedName")) {
            BloodSugar bloodSugar = bloodSugarService.queryData(map.get("id").toString());
            if (bloodSugar != null) {
                bloodSugar.setStatus((int) map.get("status"));
                bloodSugarService.update(bloodSugar);
                json.put("msg", "编辑成功");
            } else {
                json.put("msg", "编辑失败，没有这个对象。");
                return json.toString();
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());

        return json.toString();
    }

    /**
     * 删除血糖表记录
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "删除失败");
        if (map.containsKey("id")) {
            BloodSugar bloodSugar = bloodSugarService.queryData(map.get("id").toString());
            if (bloodSugar != null) {
                bloodSugar.setStatus(-1);
                bloodSugarService.update(bloodSugar);
                json.put("msg", "删除成功");
            } else {
                json.put("msg", "删除失败，没有这个对象。");
                return json.toString();
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据主键id查询血糖表记录
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/queryData", method = RequestMethod.POST)
    @ResponseBody
    public String queryData(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        if (map != null && map.containsKey("id")) {
            BloodSugar bloodSugar = bloodSugarService.queryData(map.get("id").toString());
            if (bloodSugar != null) {
                json.put("msg", "查询成功");
                json.put("data", JSONObject.fromObject(bloodSugar));
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据条件分页查询血糖表记录列表（也可以不分页）
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public String queryList(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        if (map != null && map.containsKey("start")) {
            int totalCount = bloodSugarService.queryNum(map);
            map.put("start", ((int) map.get("start") - 1) * (int) map.get("size"));
            json.put("totalCount", totalCount);
        }
        List<BloodSugar> list = bloodSugarService.queryList(map);
        if (list != null && list.size() > 0) {
            json.put("msg", "查询成功");
            json.put("data", JSONArray.fromObject(list));
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据时间查询在科病人的血糖信息
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/getListByTime", method = RequestMethod.POST)
    @ResponseBody
    public String getListByTime(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        List<Map<String, Object>> list = bloodSugarService.getListByTime(map);
        if (list != null && list.size() > 0) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.registerJsonValueProcessor(Timestamp.class, new JsonArrayValueProcessor());
            JSONArray array = JSONArray.fromObject(list, jsonConfig);
            json.put("data", array);
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "查询成功");
        }
        return json.toString();
    }

    /**
     * 护理文书--血糖监测--保存一条数据
     *
     * @param bloodSugar
     * @return
     */
   /* @RequestMapping(value = "/saveData",method= RequestMethod.POST)
    @ResponseBody*/
    public String saveOne(BloodSugar bloodSugar, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "保存失败");
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("status", 1);
            map.put("patientId", bloodSugar.getPatientId());
            map.put("code", bloodSugar.getCode());
            map.put("dataTime", format.format(bloodSugar.getDataTime()));
            BloodSugar data = bloodSugarService.queryDataByCodeAndPatientId(map);
            if (StringUtils.isNotEmpty(bloodSugar.getPatientId())) {
                if (data != null) {
                    //不等于空则编辑
                    data.setContent(bloodSugar.getContent());
                    data.setCode(bloodSugar.getCode());
                    bloodSugarService.update(data);
                } else {
                    //等于空则新增
                    bloodSugar.setId(UUIDUtil.getUUID());
                    bloodSugar.setCreateTime(new Date());
                    User user = (User) session.getAttribute("user");
                    if (user != null) {
                        bloodSugar.setOperatorCode(user.getLoginName());
                    }
                    bloodSugarService.insert(bloodSugar);
                }
                json.put("code", HttpCode.OK_CODE.getCode());
                json.put("msg", "保存成功");
            } else {
                json.put("code", HttpCode.NO_PATIENT_CODE.getCode());
                json.put("msg", "请先选择病人");
            }
        } catch (Exception e) {
            logger.info(":" + e);
        }
        return json.toString();
    }

    /**
     * 护理文书--血糖监测--删除整行
     * @param map
     * @return
     */
    /*@RequestMapping(value = "/deleteData")
    @ResponseBody
    public String deleteData(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("msg","删除失败");
            List<BloodSugar> list=bloodSugarService.queryListByTimeAndPatientId(map);
            if(list!=null && list.size()>0){
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setStatus(-1);
                    bloodSugarService.delete(list.get(i));
                }
                json.put("code",HttpCode.OK_CODE.getCode());
                json.put("msg","删除成功");
            }
        return json.toString();
    }*/

    /**
     * 护理文书--血糖监测--根据病人id 和 时间查询血糖列表
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/getList")
    @ResponseBody
    public String getList(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "暂无数据");
        if (map != null && map.containsKey("patientId")) {
            System.out.println("参数值为：" + JSON.toJSON(map));
            List<Map<String, Object>> list = bloodSugarService.getList(map);
            if (list != null && list.size() > 0) {
                json.put("data", JSONArray.fromObject(list));
                json.put("code", HttpCode.OK_CODE.getCode());
                json.put("msg", "查询成功");
            }
        } else {
            json.put("code", HttpCode.NO_PATIENT_CODE.getCode());
            json.put("msg", "请先选择病人");
        }
        return json.toString();
    }

    /**
     * 护理文书--血糖监测--保存一条数据
     *
     * @return
     */
    @RequestMapping(value = "/saveData", method = RequestMethod.POST)
    @ResponseBody
    public String saveData(@RequestBody(required = false) Map<String, Object> nursingmap, HttpSession session) {
        User user = (User) session.getAttribute("user");
        JSONArray array = JSONArray.fromObject(nursingmap.get("list").toString());
        JSONObject jsonObject = (JSONObject) array.get(0);
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "保存失败");

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", 1);
        map.put("patientId", jsonObject.get("patientId").toString());
        map.put("dataTime", jsonObject.get("dataTime").toString());
        BloodSugar data = bloodSugarService.queryDataByCodeAndPatientId(map);
        if (jsonObject.get("id").toString().equals("")) {//无id，先判断当前时间（精确到分）是否有数据，有数据提示无法新增，无数据则正常新增
            if (data != null) {
                json.put("msg", "检测到录入时间重复");
                return json.toString();
            } else {
                BloodSugar bloodSugar = new BloodSugar();
                bloodSugar.setId(jsonObject.get("id").toString());
                bloodSugar.setContent(jsonObject.get("dosage").toString());
                bloodSugar.setCode(jsonObject.get("bloodSugarType").toString());
                bloodSugar.setSignature(jsonObject.get("signature").toString());
                bloodSugar.setVisitCode(jsonObject.get("visitCode").toString());
                bloodSugar.setVisitId(jsonObject.get("visitId").toString());
                bloodSugar.setPatientId(jsonObject.get("patientId").toString());
                bloodSugar.setStatus(Integer.valueOf(jsonObject.get("status").toString()));
                try {
                    bloodSugar.setDataTime(DateUtil.yyyyMMddHHmmSdfToDate(jsonObject.get("dataTime").toString()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                bloodSugar.setId(UUIDUtil.getUUID());
                bloodSugar.setCreateTime(new Date());
                bloodSugar.setOperatorCode(user.getLoginName());
                bloodSugarService.insert(bloodSugar);
                json.put("msg", "保存成功");
            }
        } else {//有id，修改
            Map<String, Object> selectmap = new HashMap<String, Object>();
            selectmap.put("id", jsonObject.get("id").toString());
            BloodSugar bloodSugar = bloodSugarService.queryDataById(selectmap);
            bloodSugar.setContent(jsonObject.get("dosage").toString());
            bloodSugar.setCode(jsonObject.get("bloodSugarType").toString());
            bloodSugar.setSignature(jsonObject.get("signature").toString());
            bloodSugar.setId(jsonObject.get("id").toString());
            try {
                bloodSugar.setDataTime(DateUtil.yyyyMMddHHmmssSdfToDate(jsonObject.get("dataTime").toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            bloodSugarService.update(bloodSugar);
            json.put("msg", "保存成功");
        }

        return json.toString();

    }


    @RequestMapping(value = "/isRepetition", method = RequestMethod.POST)
    @ResponseBody
    public String isRepetition(@RequestBody(required = false) Map<String, Object> nursingmap, HttpSession session) {
        User user = (User) session.getAttribute("user");
        JSONArray array = JSONArray.fromObject(nursingmap.get("list").toString());
        JSONObject jsonObject = (JSONObject) array.get(0);
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "successful");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", 1);
        map.put("patientId", jsonObject.get("patientId").toString());
        map.put("dataTime", jsonObject.get("dataTime").toString());
        BloodSugar data = bloodSugarService.queryDataByCodeAndPatientId(map);
        if (jsonObject.get("id").toString().equals("")) {//无id，先判断当前时间（精确到分）是否有数据，有数据提示无法新增，无数据则正常新增
            if (data != null) {
                json.put("msg", "检测到录入时间重复");
                return json.toString();
            }
        }
        return json.toString();
    }


    /**
     * 护理单-血糖监测新增
     *
     * @return
     */
    @RequestMapping(value = "/insertData", method = RequestMethod.POST)
    @ResponseBody
    public String insertData(@RequestBody(required = false) Map<String, Object> nursingmap, HttpSession session) {
        User user = (User) session.getAttribute("user");
        JSONArray array = JSONArray.fromObject(nursingmap.get("list").toString());
        JSONObject jsonObject = (JSONObject) array.get(0);
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "保存失败");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", 1);
        map.put("patientId", jsonObject.get("patientId").toString());
        map.put("dataTime", jsonObject.get("dataTime").toString());
        BloodSugar data = bloodSugarService.queryDataByCodeAndPatientId(map);
        if (data != null) {
            data.setContent(jsonObject.get("dosage").toString());
            data.setCode(jsonObject.get("bloodSugarType").toString());
            bloodSugarService.update(data);
            json.put("msg", "保存成功");
        } else {
            BloodSugar bloodSugar = new BloodSugar();
            bloodSugar.setId(UUIDUtil.getUUID());
            bloodSugar.setContent(jsonObject.get("dosage").toString());
            bloodSugar.setCode(jsonObject.get("bloodSugarType").toString());
            bloodSugar.setSignature(user.getLoginName());
            bloodSugar.setVisitCode(jsonObject.get("visitCode").toString());
            bloodSugar.setVisitId(jsonObject.get("visitId").toString());
            bloodSugar.setPatientId(jsonObject.get("patientId").toString());
            bloodSugar.setStatus(1);
            try {
                bloodSugar.setDataTime(DateUtil.yyyyMMddHHmmssSdfToDate(jsonObject.get("dataTime").toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            bloodSugar.setId(UUIDUtil.getUUID());
            bloodSugar.setCreateTime(new Date());
            bloodSugar.setOperatorCode(user.getLoginName());
            bloodSugarService.insert(bloodSugar);
            json.put("msg", "保存成功");

        }
        return json.toString();
    }

    /**
     * 新增
     */
    @ApiOperation("血糖添加修改")
    @RequestMapping(value = "/insertNew", method = RequestMethod.POST)
    @ResponseBody
    public String insertNew(@RequestBody BloodSugarDTO bloodSugarNew, HttpSession session) {
        /**
         * map 中存在的  rowId   判断一组数据
         * 血糖的数组
         *
         */
        User user = (User) session.getAttribute("user");
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "操作失败");
        try {
            //String rowId = bloodSugarDTO.getId();
            String patientId = bloodSugarNew.getPatientId();
            List<BloodSugarDTO> list = bloodSugarNew.getList();
            if (list != null && list.size() > 0) {
                //修改
                for (BloodSugarDTO bloodSugarDTO : list) {
                    if (bloodSugarDTO.getId() != null && !"".equals(bloodSugarDTO.getId())) {

                        BloodSugar bloodSugar1 = bloodSugarService.queryData(bloodSugarDTO.getId());
                        bloodSugar1.setPatientId(patientId);
                        bloodSugar1.setDataTime(DateUtil.yyyyMMddHHmmssSdfToDate(bloodSugarDTO.getDataTime()));
                        bloodSugar1.setCreateTime(new Date());
                        bloodSugar1.setOperatorCode(user.getUserName());
                        bloodSugar1.setCode(bloodSugarDTO.getCode());
                        bloodSugar1.setContent(bloodSugarDTO.getContent());
                        bloodSugar1.setSignature(bloodSugarDTO.getSignature());
                        bloodSugar1.setStatus(1);
                        bloodSugar1.setRelevancyId(bloodSugarDTO.getRelevancyId());
                        bloodSugarService.update(bloodSugar1);
                    } else {
                        if ((bloodSugarDTO.getCode() != "" && bloodSugarDTO.getCode() != null) && (bloodSugarDTO.getSignature() != "" && bloodSugarDTO.getSignature() != null) && (bloodSugarDTO.getDataTime() != "" && bloodSugarDTO.getDataTime() != null) && (bloodSugarDTO.getContent() != "" && bloodSugarDTO.getContent() != null)) {
                            BloodSugar bloodSugar = new BloodSugar();
                            bloodSugar.setId(UUIDUtil.getUUID());
                            bloodSugar.setPatientId(patientId);
                            bloodSugar.setCode(bloodSugarDTO.getCode());
                            bloodSugar.setSignature(bloodSugarDTO.getSignature());
                            bloodSugar.setContent(bloodSugarDTO.getContent());
                            bloodSugar.setDataTime(DateUtil.yyyyMMddHHmmssSdfToDate(bloodSugarDTO.getDataTime()));
                            bloodSugar.setCreateTime(new Date());
                            bloodSugar.setOperatorCode(user.getUserName());
                            bloodSugar.setStatus(1);
                            bloodSugar.setRelevancyId(bloodSugarDTO.getRelevancyId());
                            bloodSugarService.insert(bloodSugar);
                        }
                    }

                }
            }
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "操作成功");

        } catch (Exception e) {
            logger.info("增加一条血糖表记录:" + e);
        }
        return json.toString();
    }

    /**
     * 删除
     */
    @ApiOperation("通过id删除一条血糖数据")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult deleteById(@RequestBody Map<String, Object> map) {
        try {
            errorIfEmpty(map, "参数不能为空");
            errorIfEmpty(map.get("id"), "删除数据id不能为空");
            BloodSugar bloodSugar = bloodSugarService.queryData((String) map.get("id"));
            errorIfEmpty(bloodSugar, "需要删除数据不存在");
            bloodSugar.setStatus(-1);
            bloodSugarService.update(bloodSugar);
            return new HttpResult(200, "数据删除成功");
        } catch (Exception e) {
            return new HttpResult(500, "删除失败：" + e.getMessage());
        }
    }

    /**
     * 删除
     */
    @ApiOperation("血糖删除")
    @RequestMapping(value = "/deleteNew", method = RequestMethod.POST)
    @ResponseBody
    public String deleteNew(@RequestBody Map<String, Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "删除失败");
        try {
            String rowId = map.get("rowId").toString();
            //通过rowid删除数据
            List<BloodSugar> bloodSugars = bloodSugarService.queryListByRowId(rowId);
            for (BloodSugar bloodSugar : bloodSugars) {
                bloodSugar.setStatus(-1);
                bloodSugarService.delete(bloodSugar);
            }
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "删除成功");

        } catch (Exception e) {
            logger.info("删除一条血糖表记录:" + e);
        }
        return json.toString();
    }

    /**
     * 列表
     */
    @ApiOperation("获取血糖的列表")
    @RequestMapping(value = "/queryDataNew", method = RequestMethod.POST)
    @ResponseBody
    public String queryDataNew(@RequestBody Map<String, Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "查询列表失败");
        try {
            String patientId = map.get("patientId").toString();
            //通过rowid分组查询列表
            List<BloodSugar> bloodSugars = bloodSugarService.queryDataNew(patientId);

            String arr = JSON.toJSONString(bloodSugars);
            json.put("data", arr);
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "查询列表成功");

        } catch (Exception e) {
            logger.info("查询列表血糖表记录:" + e);
        }
        return json.toString();
    }

    @ApiOperation("通过rowId获取单个血糖页面数据")
    @RequestMapping(value = "/queryRowDataNew", method = RequestMethod.POST)
    @ResponseBody
    public String queryRowDataNew(@RequestBody Map<String, Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "查询数据失败");
        try {
            //通过rowid分组查询列表
            String rowId = map.get("rowId").toString();
            List<BloodSugar> bloodSugars = bloodSugarService.queryListByRowId(rowId);
            String arr = JSON.toJSONString(bloodSugars);
            json.put("data", arr);
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "查询数据成功");

        } catch (Exception e) {
            logger.info("查询血糖表记录:" + e);
        }
        return json.toString();
    }

    /**
     * 按前台页面的表格条数和页码数查询数据
     */
    @ApiOperation("查询最新血糖页面数据")
    @RequestMapping(value = "/queryListByPatientIdAndPageSize", method = RequestMethod.POST)
    @ResponseBody
    public String queryListByPatientIdAndPageSize(@RequestBody Map<String, Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "查询数据失败");
        try {
            Map<String, Object> mapData = bloodSugarService.queryDataByPageAndPatientId(map);
            String arr = JSON.toJSONString(mapData);
            json.put("data", arr);
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "查询数据成功");
        } catch (Exception e) {
            logger.info("查询血糖表记录:" + e);
        }
        return json.toString();
    }

}
